package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Employee;
import com.sky.entity.Orders;
import com.sky.vo.OrderReportVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Classname OrderMapper
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/7/5 13:50
 * @Created by Ren
 */
@Mapper
public interface OrderMapper {


    /**
     * 插入订单数据
     *
     * @param order
     */
    void insert(Orders order);

    Page<Orders> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单号查询订单
     *
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where id = #{id}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, Long id);


    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 管理员根据条件查询订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

    Integer getOrderStatisticsByStatus(Integer status);

    /**
     * 查询待付款且时间超时的数据
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> processTimeoutOrder(Integer status, LocalDateTime orderTime);

    List<Orders> getTurnoverStatistics(LocalDateTime begin, LocalDateTime end);

    OrderReportVO getTotalAndValidOrders(LocalDateTime beginDateTime, LocalDateTime endDateTime);

    @Select("select count(*) from orders where DATE(order_time) = #{dateTime}")
    Long getOrderCountByDate(LocalDate dateTime);

    @Select("select count(*) from orders where DATE(order_time) = #{dateTime} and status = 5")
    Long getGetValidOrderCount(LocalDate date);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);


    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);
}
